package cn.ldf.keepaccounts.controller;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjUtil;
import cn.ldf.keepaccounts.entity.dto.RoomsDTO;
import cn.ldf.keepaccounts.entity.dto.RoomsHistoryDTO;
import cn.ldf.keepaccounts.entity.pojo.Rooms;
import cn.ldf.keepaccounts.entity.vo.DissolutionMemberOneVO;
import cn.ldf.keepaccounts.entity.vo.R;
import cn.ldf.keepaccounts.service.RoomsService;
import cn.ldf.keepaccounts.utils.ExceptionUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;

/**
 * 房间表(Rooms)表控制层
 *
 * @author 清峰
 * @since 2025-02-17 10:15:14
 */
@RestController
@RequestMapping("rooms")
public class RoomsController {

    @Resource
    private RoomsService roomsService;

    /**
     * 创建房间
     *
     * @param roomsDTO 实体对象
     * @return 新增结果
     */
    @PostMapping("/createRooms")
    public R<RoomsDTO> createRooms(@Valid @RequestBody RoomsDTO roomsDTO) {
        ExceptionUtil.throwMsg(() -> ObjUtil.isEmpty(roomsDTO.getRooms()), "房间信息不能为空");
        ExceptionUtil.throwMsg(() -> CollUtil.isEmpty(roomsDTO.getMembers()), "房间成员不能为空");
        return R.success(roomsService.createRooms(roomsDTO));
    }

    /**
     * 结算房间
     *
     * @param roomId 房间ID，通过
     */
    @GetMapping("/dissolutionRooms")
    public R<Boolean> dissolutionRooms(@RequestParam String roomId) {
        return R.success(roomsService.dissolutionRooms(roomId));
    }

    /**
     * 房间成员个人结算
     *
     * @param roomId 房间id
     * @param openId 用户id
     * @return 结果集
     */
    @GetMapping("/dissolutionRoomsToMember")
    public R<DissolutionMemberOneVO> dissolutionRoomsToMember(@RequestParam String roomId, @RequestParam String openId) {
        return R.success(roomsService.dissolutionRoomsToMember(roomId, openId));
    }

    /**
     * 根据房间ID查询房间是否存在
     *
     * @param roomId 房间ID，通过URL路径变量传递
     * @return 返回一个封装了房间信息的响应对象
     */
    @GetMapping("/isExistByRoomId/{roomId}")
    public R<Boolean> isExistByRoomId(@PathVariable String roomId) {
        return R.success(roomsService.isExistByRoomId(roomId));
    }

    /**
     * 根据房间ID查询房间是否已经结算
     *
     * @param roomId 房间ID，通过URL路径变量传递
     * @return 返回一个封装了房间信息的响应对象
     */
    @GetMapping("/isDissolutionRooms/{roomId}")
    public R<Boolean> isDissolutionRooms(@PathVariable String roomId) {
        return R.success(roomsService.isDissolutionRooms(roomId));
    }

    /**
     * 根据房间ID获取房间信息
     *
     * @param roomId 房间ID，通过URL路径变量传递
     * @return 返回一个封装了房间信息的响应对象
     */
    @GetMapping("/getByRoomId/{roomId}")
    public R<Rooms> getByRoomId(@PathVariable String roomId) {
        return R.success(roomsService.getByRoomId(roomId));
    }

    /**
     * 生成小程序码（图片流返回）
     *
     * @param roomId   房间ID
     * @param pagePath 页面路径
     */
    @GetMapping("/generateQrCode/{roomId}")
    public void generateQrCode(@PathVariable String roomId, String pagePath) {
        roomsService.generateQrCode(roomId, pagePath);
    }

    /**
     * 分页查询房间结算历史列表
     *
     * @param page 分页对象
     * @return 房间解散历史
     */
    @GetMapping("/listByPageDissolutionHistory")
    public R<Page<RoomsHistoryDTO>> listByPageDissolutionHistory(Page<Rooms> page, Rooms rooms) {
        return R.success(roomsService.dissolutionHistory(page, rooms));
    }

    /**
     * 根据openId查询加入且正在进行的房间
     * 用户未结算房间
     * @param openId openId
     * @return 结果集
     */
    @GetMapping("/getDoingRoomByOpenId")
    public R<Rooms> getDoingRoomByOpenId(@RequestParam String openId) {
        return R.success(roomsService.getDoingRoomByOpenId(openId));
    }

}

